package com.financing.service.system;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.financing.model.system.AssetsLog;
import com.financing.model.user.User;
import com.financing.service.BaseService;
import com.financing.util.Str;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
  
public class AssetsLogService extends BaseService<User>{
 
	/**
	 * 分页数据
	 */
	public Page<AssetsLog> list(int page, int size, Map<String, String> map) {
		return AssetsLog.me.paginate(page, size, buildSql("select *, contents as summary", "assets_log",Arrays.asList("user_id","agent_id"),map," order by id desc "));
	} 
	
	/**
	 * 资产信息创建
	 */
	public boolean bulid(User u, String name, String contents, String agentContents) {
		if(Str.nb(contents)) {
			AssetsLog al = new AssetsLog();
			al.setName(name);
			al.setContents(contents);
			al.setCreateTime(new Date());
			al.setUserId(u.getId());
			al.setRealName(u.getRealName());
			al.setMobile(u.getMobile());
			if(Str.nb(agentContents)){
				al.setAgentId(u.getAgentId());
				al.setAgentContents(agentContents);
			}
			return al.save();
		} else {
			return false;
		}
	}
	
	/**
	 * 总计几条数据没有读
	 */
	public long unReadNum(User u) {
		AssetsLog al = AssetsLog.me.findFirst("select count(id) as count from assets_log where is_read=0 and user_id=?", u.getId());
		return al!=null && al.getLong("count") >0 ? al.getLong("count") : 0;
	}
	
	/**
	 * 根据类型返回资产信息 
	 */ 
	public AssetsLog findByType(int userId) {
		return AssetsLog.me.findFirst("select * from assets_log where user_id=? and is_read=0 order by create_time desc",userId);
	}
	
	/**
	 * 总计几条数据没有读
	 */
	public long setAllRead(User u) {
		return Db.update("update assets_log set is_read=1 where user_id=?", u.getId());
	}
}
